﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TDUpdate;
using Microsoft.Xna.Framework.Input;
using TDDraw;
using Microsoft.Xna.Framework;

namespace TDDraw.Input
{
    public class MouseManager : IMouse
    {
        #region Singleton
        private static MouseManager instance;

        private MouseManager() { }

        public static MouseManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new MouseManager();
                }
                return instance;
            }
        }
        #endregion Singleton

        public event EventHandler<MouseStateChangedEventArgs> MouseClicked;
        MouseState OldMouseState = Mouse.GetState();

        public void Update(GameTime gameTime)
        {
            MouseState newMouseState = Mouse.GetState();
            if (newMouseState.LeftButton == ButtonState.Pressed && OldMouseState.LeftButton == ButtonState.Released)
            {
                List<ActionHappendResult> resultList = DrawManager.ActionHappend(newMouseState.X, newMouseState.Y);
                bool resultHandled = false;
                //TODO: resultList sortieren nach DrawOrder (theoretisch & real)
                foreach (ActionHappendResult result in resultList)
                {
                    if (!resultHandled)
                    {
                        EventHandler<MouseStateChangedEventArgs> temp = MouseClicked;
                        if (temp != null)
                        {
                            temp(result.sender, new MouseStateChangedEventArgs(result.innerObject, ref resultHandled));
                        }
                    }
                }
            }

            //TODO: Implement Released, DoubleClicked, Clicked&Released, Hovered, HoveredIn, HoveredOut
        }
    }
}
